package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.Clock;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import algorithm.Dynamic_credit;
import algorithm.Point_record;
import DB.DB_connection_pool;


public class Publish_review extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		JSONObject jsonObj = new JSONObject();
		
		
		String dynamic_information_id = request.getParameter("dynamic_information_id");   //动态编号
		String reviewer_account = request.getSession().getAttribute("user_account").toString();      //评论者账号
		String review_content = request.getParameter("review_content");         //评论类容
		
		Clock clock = Clock.systemUTC();
		String review_id = String.valueOf((int)(Math.random()*900000)+100000)+clock.millis();  //评论编号;
		java.sql.Date review_date = new java.sql.Date(clock.millis());     //当前时间
		
		String publish_review_sql = "insert into review(dynamic_information_id,reviewer_account,review_content,review_id,review_date) values(?,?,?,?,?)";
		String review_number = "update dynamic_information set dynamic_information_review_number = dynamic_information_review_number+1 where dynamic_information_id = '"+dynamic_information_id+"'";
		
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select = conn.prepareStatement(publish_review_sql);
			ps_select.setString(1, dynamic_information_id);
			ps_select.setString(2, reviewer_account);
			ps_select.setString(3, review_content);
			ps_select.setString(4, review_id);
			ps_select.setDate(5, review_date);
			if(ps_select.executeUpdate()!=0){    //评论成功
			//评论+1
			PreparedStatement review_up = conn.prepareStatement(review_number);
			review_up.executeUpdate();
			review_up.close();
				
			jsonObj.put("result", true);
			
//////////////////--------积分相关，由于之前没设计好，导致这一块代码耦合程度低，再次特别标出------///////////
			Dynamic_credit dc = new Dynamic_credit();
			Map<String, Integer> map = dc.Get_credit(Dynamic_credit.review);

			Point_record pr = new Point_record();
			if(map.get("flag")==1){            //积分改变
			pr.change_point(reviewer_account,(double)(map.get("credit")));
			pr.point_record(reviewer_account, Point_record.review_event, (double)(map.get("credit")), true);
			}
			pr.point_being_change(dynamic_information_id, Point_record.being_review_event);
///////////////---------end积分--------////////////////
			
			}else{
				jsonObj.put("result", false);
			}
			
			out.print(jsonObj.toString());
			
			conn.close();
			ps_select.close();
		} catch (SQLException e) {
			jsonObj.put("result", false);
			out.print(jsonObj.toString());
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}
